package com.macro.mall.tiny.modules.foster.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.foster.model.Room;
import com.macro.mall.tiny.modules.foster.service.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhuyi
 * @since 2024-03-30
 */
@Api(tags = "RoomController", description = "房间管理")
@RestController
@RequestMapping("/foster/room")
@CrossOrigin
public class RoomController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RoomController.class);
    @Autowired
    private RoomService roomService;

    /**
     * 添加房间
     *
     * @param
     * @return
     */
    @ApiOperation(value = "添加房间")
    @RequestMapping(value = "/addRoom", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult addRoom(@RequestBody Room room) {
        room.setId(getId());
        roomService.save(room);
        LOGGER.info("房间添加成功");
        return CommonResult.success("房间添加成功");
    }

    /**
     * 删除房间
     *
     * @param
     * @return
     */
    @ApiOperation(value = "删除房间")
    @RequestMapping(value = "/deleteRoomById/{id}", method = RequestMethod.DELETE)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult deleteRoom(@PathVariable Long id) {
        roomService.removeById(id);
        LOGGER.info("房间删除成功");
        return CommonResult.success("房间删除成功");
    }

    /**
     * 修改房间
     *
     * @param
     * @return
     */
    @ApiOperation(value = "修改房间")
    @RequestMapping(value = "/updateRoom", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult updateRoom(@RequestBody Room room) {
        roomService.updateById(room);
        LOGGER.info("房间修改成功");
        return CommonResult.success("房间修改成功");
    }

    /**
     * 分页查询所有种类的房间列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询所有种类的房间列表")
    @PreAuthorize("hasAuthority('admin')")
    @RequestMapping(value = "/listAllRooms", method = RequestMethod.GET)
    public CommonResult<CommonPage<Room>> listAllRooms(@RequestParam(value = "pageNum", defaultValue = "1")
                                                       @ApiParam("页码") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "5")
                                                       @ApiParam("每页数量") Integer pageSize) {
        Page<Room> page = new Page<>(pageNum, pageSize);
        Page<Room> pageResult = roomService.page(page);
        LOGGER.info("所有种类的房间列表查询状态：查询成功");
        return CommonResult.success(CommonPage.restPage(pageResult));
    }

    /**
     * 分页查询相应种类的房间列表
     *
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @ApiOperation("分页查询相应种类的房间列表")
    @PreAuthorize("hasAnyAuthority('user','admin')")
    @RequestMapping(value = "/listRoomsByType", method = RequestMethod.GET)
    public CommonResult<CommonPage<Room>> listRoomsByType(@RequestParam(value = "pageNum", defaultValue = "1")
                                                          @ApiParam("页码") Integer pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "5")
                                                          @ApiParam("每页数量") Integer pageSize,
                                                          @RequestParam(value = "type") @ApiParam("房间类型") String type) {
        Page<Room> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper
                .eq("type",type);
        Page<Room> pageResult = roomService.page(page,wrapper);
        LOGGER.info("所有种类的房间列表查询状态：查询成功");
        return CommonResult.success(CommonPage.restPage(pageResult));
    }

    /**
     * 获取指定id的房间
     * @param id
     * @return
     */
    @ApiOperation("获取指定id的房间")
    @RequestMapping(value = "/getRoomById/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('user','admin')")
    public CommonResult<Room> getUserById(@PathVariable("id") Long id) {
        Room room = roomService.getById(id);
        LOGGER.info("相应种类的房间列表查询状态：查询成功");
        return CommonResult.success(room);
    }

}
